<html>
<head>
<title>PHPKITTEN</title>
</head>
<body>
<?php
/*
PHPKITTEN
v 0.93
Copyright (c) 2009 Ian Foster
http://code.google.com/p/phpkitten/
*/
//define some settings
$username = "username";
$password = "password";
//read only mode (0 = normal, 1 = read only)
$readonly = 1;


session_start();// start up your PHP session! 
@$user = $_POST['user'];
@$pass = $_POST['pass'];
@$filename = $_GET["file"]; //get enterd file
@$dirname = $_GET["dir"]; //get new dir
@$action = $_GET["action"]; //get action
@$sure = $_GET["sure"];
@$type = $_GET["type"]; //get file type
@$code = $_POST["code"];//get code
$me = $_SERVER['PHP_SELF']; //me, myself, xor i	

//user auth code
if( $action == "login" and trim($user) == $username and trim($pass) == $password)
{
    $_SESSION['phpcatauth'] = 1; // store session data
    $action = "";
}elseif($action == "logout")
{
    $_SESSION['phpcatauth'] = 0; // empty session data
    session_destroy();//del cookie
}
if( @$_SESSION['phpcatauth'] != 1 )
{
    echo("<h3>Need to log in</h3>\n");
    echo("<form method=\"post\" action=\"$me?action=login\">\n
    Username: <input name=\"user\" type=\"text\" />\n<br>\n
    Password: <input name=\"pass\" type=\"password\" />\n
    <br><input name=\"login\" value=\"Log in\" type=\"submit\">\n
    </form>");
    //die();
}

//phpkitten code
if($dirname == "" or $dirname == "/")
{
    $dirname = "./";
}
if(strpos($dirname,"..") !== false or strpos($filename,"../") !== false or strpos($dirname,"./") !== 0 )
{
    die("Permission Denied!");
}
if(@$_SESSION['phpcatauth'] == 1)
{
    echo("<a href=\"$me?action=logout\">Logout</a><br>");
    if( empty($action) == false )
    {
            echo("<a href=\"" . $me . "?dir=".dirname($filename)."/\">Directory</a><br>");
    
            if( $action == "view" )
            {
                $file = file($filename);
                if( $readonly == 0 )
                {
                    echo(" <a href=\"" . $me . "?action=edit&file=$filename\">Edit</a>");
                    echo(" <a href=\"" . $me . "?action=delete&file=$filename\">Delete</a>\n<br>\n");
                }
                echo "Viewing: $filename\n<br><br>\n";
                    $count = 1;
                    foreach($file as $key => $line)
                {
                    $html = htmlentities($line);
                    $htmltab = str_replace("\t","    ",$html); 
                    $htmlspaces = str_replace("    ","&nbsp;&nbsp;&nbsp;&nbsp;",$htmltab);
                    $htmlspacesnl = nl2br($htmlspaces);
            
                    echo $count . ": " . $htmlspacesnl;
                    $count++;
                }
                echo "\n<br>\n";
            }elseif( $action == "edit" && $readonly == 0 )
            {
                echo(" <a href=\"" . $me . "?action=view&file=$filename\">View</a>");
                echo(" <a href=\"" . $me . "?action=delete&file=$filename\">Delete</a>\n<br>\n");
                echo "Editing: $filename\n<br>\n";
                //end php for html form box
                $filedata = file_get_contents($filename);
                $cfiledata = htmlspecialchars($filedata,ENT_NOQUOTES); 
                ?>
                
                <form action="<?php echo($me."?action=save&file=".$filename); ?>" method="post">
                <textarea name="code"style="width: 100%; height: 85%"><?php echo $cfiledata; ?></textarea>
                <input type="submit" value="Save" />
                <input type="reset" value="Reset" />
                <br>
    
                </form>
    
                
                <?php
                //resume main php code
            }elseif( $action == "save" && $readonly == 0 )
            {
                echo("<a href=\"" . $me . "?action=view&file=$filename\">View</a>\n<br>\n");
                echo("Saving " . $filename . "<br>");
                $fh = fopen($filename, 'w') or die("Can't open file, bad permissions");
                fwrite($fh, $code);
                fclose($fh);
                echo("Done Saving");
    
            }elseif( $action == "new" && $readonly == 0 )
            {
                if( $type == "file" )
                {
                    echo("Making new file: " . $filename);
			        touch($filename);
			        echo("\n<br>\nFile Created");
		    	}elseif( $type == "dir" )
		    	{
                    echo("Making new dir: " . $filename);
		    	    mkdir($filename);
		    	    echo("\n<br>\nDir Created");
		    	}else
		    	{
		    	    echo"File or Dir?";
		    	}
            }elseif( $action == "delete" && $readonly == 0 )
	    	{
	    		if( empty($filename) !== true )
	    		{
	    		    if($sure==yes)
	    		    {
	    		        echo( "Deleting: " . $filename . "\n<br>\n");
	    		        unlink($filename) or die("Some del file error");
	    		        echo("\n<br>\nDone<br>");
	    		    }else
	    		    {
	    		    	echo "<br>Are you sure you want to delete $filename?\n<br>\n";
	    		        echo"<a href=\"" . $me . "?dir=".dirname($filename)."/\">No</a> <a href=\"$me?action=delete&file=$filename&sure=yes\">Yes</a>\n<br>\n";
	    		    }
	    		}elseif( empty($dirname) !== true )
	    		{
		    	    if($sure==yes)
		    	    {
			            echo( "Deleting: " . $dirname . "\n<br>\n");
			            rmdir($dirname) or die("Some del dir error");
			            echo("\n<br>\nDone<br>");
			        }else
			        {
			        	echo "<br>Are you sure you want to delete $dirname?\n<br>\n";
			            echo"<a href=\"" . $me . "?dir=".dirname($filename)."/\">No</a> <a href=\"$me?action=delete&dir=$dirname&sure=yes\">Yes</a>\n<br>\n";
			        }
			    }else
		    	{
		    	    echo"File or Dir?";
		    	}
	    	}else
            {
	    		echo "Bad command\n";
            }
    }else
    {
    
            echo "<br>Current Dir " . $dirname;
            if( $readonly == 0 )
            {
                if($dirname != "./")
                {
                    echo "&nbsp;&nbsp;<a href=\"" . $me . "?action=delete&dir=$dirname\">Delete $dirname</a><br>\n";
                }
                echo("<form method=\"get\">\n
                <input name=\"type\" type=\"radio\" checked value=\"file\" />File <input name=\"type\" type=\"radio\" value=\"dir\" />Dir\n
                <input name=\"action\" type=\"hidden\" value=\"new\" />\n
                name: <input name=\"file\" value=\"$dirname\" type=\"text\" size=8 />\n
                <input value=\"Create\" type=\"submit\" />\n
                </form>");
            }
            $dir = opendir($dirname);
    
            while( false != ( $files = readdir($dir) ) )
            {
                    if( is_dir($dirname . $files) == true && $files != "." && $files != "..")
                    {
                        $dirs[] = $files;
    
                    }
                    if( is_file($dirname . $files) == true )
                    {
                        $file[] = $files;
    
                    }
            }
    
            closedir($dir);
            asort($dirs);
            asort($file);
    
            foreach($dirs as $key => $option)
            {
                    $dir_list .= "<li>Dir <a href=\"" . $me . "?dir=$dirname$option/\">$option/</a></li>\n";
            }
        
            foreach($file as $key => $option)
            {
                    $file_list .= "<li>File <a href=\"" . $me . "?action=view&file=$dirname$option\">$option</a></li>\n";
            }
        
        
            echo("<ol>\n<li>Dir <a href=\"" . $me . "?dir=" . dirname($dirname) . "/\">..</a></li>");
            echo($dir_list . $file_list . "</ol>");
    }
}
?><br>
<font size=2 >Powered by <a href="http://code.google.com/p/phpkitten/" target="_blank">phpkitten</a></font>
</body>
</html>
